package com.ahua.mallchat.common.adapter;

import com.ahua.mallchat.common.domain.enums.system.YesOrNoEnum;
import com.ahua.mallchat.common.domain.pojo.ItemConfig;
import com.ahua.mallchat.common.domain.pojo.User;
import com.ahua.mallchat.common.domain.pojo.UserBackpack;
import com.ahua.mallchat.common.domain.vo.user.BadgeResp;
import com.ahua.mallchat.common.domain.vo.user.UserInfoResp;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.beans.BeanUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class UserAdapter {

    public static User buildUserSave(String openId) {
        return User.builder().openId(openId).build();
    }

    public static User buildFillInfoUser(Long id, WxOAuth2UserInfo userInfo) {
        User user = new User();
        user.setId(id);
        user.setName(userInfo.getNickname());
        user.setAvatar(userInfo.getHeadImgUrl());
        return user;
    }

    public static UserInfoResp buildUserInfoResp(User user, Integer modifyNameCount) {
        UserInfoResp vo = new UserInfoResp();
        BeanUtils.copyProperties(user, vo);
        vo.setModifyNameChance(modifyNameCount);
        return vo;
    }

    public static List<BadgeResp> buildBadgeResp(User user, List<UserBackpack> userObtainItemList, List<ItemConfig> itemConfigList) {

        Set<Long> userObtainItemIdList = userObtainItemList.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());

        return itemConfigList.stream().map(item -> {
                    BadgeResp vo = new BadgeResp();
                    BeanUtils.copyProperties(item, vo);
                    vo.setObtain(userObtainItemIdList.contains(item.getId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
                    vo.setWearing(item.getId().equals(user.getItemId()) ? YesOrNoEnum.YES.getStatus() : YesOrNoEnum.NO.getStatus());
                    return vo;
                }).sorted(Comparator.comparing(BadgeResp::getWearing, Comparator.reverseOrder())
                        .thenComparing(BadgeResp::getObtain, Comparator.reverseOrder()))
                .collect(Collectors.toList());
    }
}
